<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * cooperation short summary.
 *
 * cooperation description.
 *
 * @version 1.0
 * @author xianbin
 */
class cooperation extends Menu_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->Model('Cooperation_model');
        $this->load->Model('Contents_model');
        $this->load->Model('Menu_model');
        $this->load->library('ajaxMsgModel');
        $this->load->library('form_validation');
    }

    public function cooperationlist()
    {
        $this->ajaxmsgmodel->Type='1';
        $this->Ajax_Check($this->ajaxmsgmodel);
        $cupge=$this->input->get('page');
        $cupge=empty($cupge) || $cupge=="0"?1:$cupge;
        $menuid=$this->uri->segment(4);

        $data['crumbs_items']=$this->get_crumbs_content_menu($menuid);
        $pagesize=15;
        $cooperation_page_objs=$this->Cooperation_model->get_page($cupge,$pagesize,"a.*","cooperation","order by weight desc, cooperation_id desc ","cooperation_id","where menuid=$menuid","");
        // p($cooperation_page_objs['objlist']);
        $this->load->library('page');
        $p=new Page();
        $data['menuid']=$menuid;
        $data['cooperationlist']=arrayToObject($cooperation_page_objs['objlist']);
        $p->init($cooperation_page_objs['count'][0]->_count,5,$cupge,$pagesize,site_url('admin/cooperation/cooperationlist/'.$menuid));
        $data['links']=$p->showPages(1);
        $this->load->view('admin/cooperation/cooperationlist.html',$data);
    }

    public function addcooperation()
    {
        $this->ajaxmsgmodel->Type='1';
        $this->Ajax_Check($this->ajaxmsgmodel);
        $menuid=$this->uri->segment(4);
        $data['crumbs_items']=$this->get_crumbs_content_menu($menuid);
        $data['menuid']=$menuid;
        $this->load->view('admin/cooperation/addcooperation.html',$data);
    }

    public function add_cooperation()
    {
        $this->ajaxmsgmodel->Type='4';
        $this->Ajax_Check($this->ajaxmsgmodel);

        $cooperationModel= new $this->Cooperation_model;
        $cooperationModel->title=$this->input->post('title');
        $cooperationModel->contents=$this->input->post('contents',false);
        $cooperationModel->imgurl=$this->input->post('imgurl');
        $cooperationModel->menuid=$this->input->post('menuid');
        $cooperationModel->weight=$this->input->post('weight');

        $res=$this->Cooperation_model->add_cooperation($cooperationModel);
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='添加成功';
            $this->ajaxmsgmodel->Statu='ok';
        }
        else
        {
            $this->ajaxmsgmodel->Msg='添加失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function editcooperation()
    {
        $this->ajaxmsgmodel->Type='1';
        $this->Ajax_Check($this->ajaxmsgmodel);
        $cooperation_id=$this->uri->segment(4);
        $cooperation_model=$this->Cooperation_model->get_cooperation_by_id($cooperation_id);
        $data['cooperationModel']=$cooperation_model;
        $data['crumbs_items']=$this->get_crumbs_content_menu($cooperation_model->menuid);
        $data['menuid']=$cooperation_model->menuid;
        $this->load->view('admin/cooperation/editcooperation.html',$data);
    }

    public function edit_cooperation()
    {
        $this->ajaxmsgmodel->Type='4';
        $this->Ajax_Check($this->ajaxmsgmodel);

        $cooperationModel= new $this->Cooperation_model;
        $cooperationModel->title=$this->input->post('title');
        $cooperationModel->contents=$this->input->post('contents',false);
        $cooperationModel->imgurl=$this->input->post('imgurl');
        $cooperationModel->cooperation_id=$this->input->post('cooperation_id');
        $cooperationModel->weight=$this->input->post('weight');

        $res=$this->Cooperation_model->update_cooperation($cooperationModel);
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='修改成功';
            $this->ajaxmsgmodel->Statu='ok';
        }
        else
        {
            $this->ajaxmsgmodel->Msg='修改失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function set_cooperation_weight()
    {
        $this->ajaxmsgmodel->Type='4';
        $this->Ajax_Check($this->ajaxmsgmodel);

        $this->load->library('ajaxMsgModel');
        $cooperationModel=new $this->Cooperation_model();
        $cooperationModel->cooperation_id=$this->uri->segment(4);
        $cooperationModel->weight=$this->input->post('weight');
        $backurl=$this->input->get('returnurl');
        $res=$cooperationModel->set_new_weight($cooperationModel);
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='更新成功';
            $this->ajaxmsgmodel->Statu='ok';
            $this->ajaxmsgmodel->BackUrl=$backurl;
        }
        else
        {
            $this->ajaxmsgmodel->Msg='更新失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function del_cooperation()
    {
        $this->ajaxmsgmodel->Type='4';
        $this->Ajax_Check($this->ajaxmsgmodel);

        $cooperation_id=$this->uri->segment(4);
        $back_url=$this->input->get('returnurl');
        $res=$this->Cooperation_model->del_cooperation($cooperation_id);
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='删除成功';
            $this->ajaxmsgmodel->Statu='ok';
            $this->ajaxmsgmodel->BackUrl=$back_url;
        }else{
            $this->ajaxmsgmodel->Msg='删除失败';
            $this->ajaxmsgmodel->Statu='ok';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function batch_del_cooperation()
    {
        $this->ajaxmsgmodel->Type='4';
        $this->Ajax_Check($this->ajaxmsgmodel);
        $nids=$this->input->post('items');
        $backurl=$this->input->get('returnurl');
        $cooperationModel=new $this->Cooperation_model();
        $nidsArray=explode('|',trim($nids,'|'));
        if(count($nidsArray)>0)
        {
            $res=$cooperationModel->batch_del_cooperation($nidsArray);
            if($res>0)
            {
                $this->ajaxmsgmodel->Msg='删除成功';
                $this->ajaxmsgmodel->Statu='ok';
                $this->ajaxmsgmodel->BackUrl=$backurl;
            }
            else
            {
                $this->ajaxmsgmodel->Msg='删除失败';
                $this->ajaxmsgmodel->Statu='err';
            }
        }
        else
        {
            $this->ajaxmsgmodel->Msg='请选择删除项！';
            $this->ajaxmsgmodel->Statu='err';
        }

        echo(json_encode($this->ajaxmsgmodel));
    }

    public function cooperationinfo()
    {
        $this->ajaxmsgmodel->Type='1';
        $this->Ajax_Check($this->ajaxmsgmodel);

        $menuid=$this->uri->segment(4);
        $data['menuid']=$menuid;
        $data['crumbs_items']=$this->get_crumbs_content_menu($menuid);
        $data['contentsModel']=$this->Contents_model->get_first_row_by_menuid($menuid);
        $this->load->view('admin/contents/contentsinfo.html',$data);
    }

    public function edit_contents()
    {
        $this->ajaxmsgmodel->Type='4';
        $this->Ajax_Check($this->ajaxmsgmodel);

        $this->load->library('ajaxMsgModel');
        $contentsModel=new $this->Contents_model();

        $contentsModel->title=$this->input->post('title');
        $contentsModel->keyword=$this->input->post('keyword');
        $contentsModel->description=$this->input->post('description');
        $contentsModel->imgurl=$this->input->post('imgurl');
        $contentsModel->contents=$this->input->post('contents',false);
        $contentsModel->menuid=$this->input->post('menuid');
        $parent_menu_model=$this->Menu_model->get_menu_by_id($contentsModel->menuid);
        $back_url=site_url('admin/'.$parent_menu_model->menu_control.'/'.$parent_menu_model->menu_action.($parent_menu_model->page_type==1?'list':'info').'/'.$parent_menu_model->menuid);
        if($this->input->post('mark')=="1")//修改
        {
            $contentsModel->contents_id=$this->input->post('contents_id');
            $res= $this->Contents_model->update_contents($contentsModel);
            if($res>0)
            {
                $this->ajaxmsgmodel->Msg='修改成功';
                $this->ajaxmsgmodel->Statu='ok';
                $this->ajaxmsgmodel->BackUrl=$back_url;
            }
            else
            {
                $this->ajaxmsgmodel->Msg='修改失败';
                $this->ajaxmsgmodel->Statu='err';
            }
        }else{//添加
            $res= $this->Contents_model->add_contents($contentsModel);
            if($res>0)
            {
                $this->ajaxmsgmodel->Msg='添加成功';
                $this->ajaxmsgmodel->Statu='ok';
                $this->ajaxmsgmodel->BackUrl=$back_url;
            }
            else
            {
                $this->ajaxmsgmodel->Msg='添加失败';
                $this->ajaxmsgmodel->Statu='err';
            }
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function del_contents()
    {
        $this->ajaxmsgmodel->Type='4';
        $this->Ajax_Check($this->ajaxmsgmodel);

        $this->load->library('ajaxMsgModel');
        $contents_id=$this->uri->segment(4);
        $contents_model=$this->Contents_model->get_contents_by_id($contents_id);
        $parent_menu_model=$this->Menu_model->get_menu_by_id($contents_model->menuid);
        $back_url=site_url('admin/'.$parent_menu_model->menu_control.'/'.$parent_menu_model->menu_action.($parent_menu_model->page_type==1?'list':'info').'/'.$parent_menu_model->menuid);
        $res=$this->Contents_model->del_contents($contents_id);
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='删除成功';
            $this->ajaxmsgmodel->Statu='ok';
            $this->ajaxmsgmodel->BackUrl=$back_url;
        }
        else
        {
            $this->ajaxmsgmodel->Msg='删除失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }
}